// pages/user/set/order/order.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: 0,
    num: '',
    itemList: [],
    noMore: 0,
    page: 1,
  },
  cut(e) {
    let num = e.currentTarget.dataset.num;
    this.setData({
      type: num,
      page: 1,
      itemList: []
    })
    this.getList();
  },
  getList() {
    let page = this.data.page
    let itemList = this.data.itemList
    let num = this.data.num;
    let url
    if (num == 4) {
      url = '/user/dyorderlist';
    } else if (num == 5) {
      url = '/user/dyorderlist2';
    }
    app.util.request({
      url: url,
      data: {
        page: page,
        status: this.data.type
      }
    }).then((res) => {
      if (res.data.length > 0) {
        this.setData({
          itemList: itemList.concat(res.data),
          noMore: 0,
          page: page + 1
        });
      } else {
        this.setData({
          noMore: 1
        });
      }
    });
  },
  detail(e) {
    let kdinfo = JSON.parse(e.currentTarget.dataset.kdinfo);
    console.log(kdinfo)
    if(this.data.type == 2 || this.data.type == 3){
      app.util.message('取货码：'+kdinfo.fetch_code, 'error', function () {
     
      })
    }
  },
  delete(e) {
    let id = e.currentTarget.dataset.id;
    app.util.request({
      url: '/user/order_del',
      data: {
        id: id
      }
    }).then((res) => {
      app.util.message(res.message)
      this.refreshList()
    });
  },
  pay(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    app.util.request({
      url: '/user/pay2',
      data: {
        id: id
      }
    }).then((res) => {
      wx.requestPayment({
        timeStamp: res.data.timeStamp,
        nonceStr: res.data.nonceStr,
        package: res.data.package,
        signType: 'MD5',
        paySign: res.data.paySign,
        success: function (res) {
          app.util.message('支付成功', 'error', function () {
            that.data.type = 1;
            that.refreshList();
          })
        },
        fail: function (res) {
          app.util.message('支付失败，请重试', 'error');
        }
      })
    });
  },
  refreshList: function () {
    this.setData({
      page: 1,
      itemList: []
    });
    this.getList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      num: options.num
    })
    this.refreshList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})